package com.fingard.util;

import java.io.*;
import java.util.LinkedList;
import java.util.List;

/**
 * CSV操作(导出和导入)
 * @author alan
 * @version 20190604
 */
public class CSVUtils {

    /**
     * 导入至.csv文件
     * @param filePath csv文件(路径+文件名)
     * @param dataList 数据
     * @return
     */
    public static boolean importCsv(String filePath, List<String> dataList,String charset){
        boolean isSucess=false;
        File file=new File(filePath);
        File paf = file.getParentFile();
        if (paf !=null && !paf.exists()) {
            paf.mkdirs();
        }
        FileOutputStream out=null;
        OutputStreamWriter osw=null;
        BufferedWriter bw=null;
        try {
            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out,charset);
            bw =new BufferedWriter(osw);
            if(dataList!=null && !dataList.isEmpty()){
                for(String data : dataList){
                    bw.append(data).append("\r");
                }
            }
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return isSucess;
    }

    /**
     * 导入至.csv文件
     * @param filePath csv文件(路径+文件名)
     * @param dataList 数据
     * @return
     */
    public static boolean importCsvIsUf8(String filePath, List<String> dataList,String charset){
        boolean isSucess=false;
        File file=new File(filePath);
        File paf = file.getParentFile();
        if (paf !=null && !paf.exists()) {
            paf.mkdirs();
        }
        FileOutputStream out=null;
        OutputStreamWriter osw=null;
        BufferedWriter bw=null;
        try {
            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out,charset);
            //osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));
            bw =new BufferedWriter(osw);
            if(dataList!=null && !dataList.isEmpty()){
                for(String data : dataList){
                    bw.append(data).append("\r");
                }
            }
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return isSucess;
    }

    /**
     * .CSV文件导出至List
     * @param filePath csv文件(路径+文件)
     * @return
     */
    public static List<String> exportCsv(String filePath,String charset){
        List<String> dataList=new LinkedList<String>();
        File file=new File(filePath);
        File paf = file.getParentFile();
        if (paf !=null && !paf.exists()) {
            paf.mkdirs();
        }
        DataInputStream in = null;
        BufferedReader br=null;
        try {
            in=new DataInputStream(new FileInputStream(file));
            br = new BufferedReader(new InputStreamReader(in,charset));
            String line = "";
            while ((line = br.readLine()) != null) {
                dataList.add(line);
            }
        }catch (Exception e) {
        }finally{
            if(in!=null){
                try {
                    in.close();
                    in=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(br!=null){
                try {
                    br.close();
                    br=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return dataList;
    }
}
